package com.wteam.Service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.wteam.PoJo.DO.Follows;
import com.wteam.PoJo.DO.user;
import com.wteam.PoJo.DTO.PageQuery;
import com.wteam.PoJo.DTO.followotherDTO;
import com.wteam.PoJo.VO.favsuserVO;
import com.wteam.mapper.FollowsMapper;
import com.wteam.Service.IFollowsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wteam.until.Result;
import com.wteam.until.UserContext;
import lombok.val;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 关注记录表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-07-16
 */
@Service
public class FollowsServiceImpl extends ServiceImpl<FollowsMapper, Follows> implements IFollowsService {

    @Override
    public Result followother(followotherDTO followotherDTO) {
        //我的id
        Long userid = UserContext.getUser();
        //被关注人的id
        Long followId = followotherDTO.getFollowId();

        Follows follows = new Follows();
        follows.setFollowId(followId);
        follows.setUserId(userid);

        save(follows);
        return Result.success("成功关注");
    }

    @Override
    public Result cancelfollow(followotherDTO followotherDTO) {
        //我的id
        Long userid = UserContext.getUser();
        //被关注人的id
        Long followId = followotherDTO.getFollowId();

        LambdaQueryWrapper<Follows> lambdaQueryWrapper = new LambdaQueryWrapper<Follows>()
                .eq(Follows::getFollowId, followId)
                .eq(Follows::getUserId, userid);
        remove(lambdaQueryWrapper);
        return Result.success("取消关注成功");
    }

    @Override
    public Result listfollow(PageQuery pageQuery) {
        Page<Follows> followsPage = pageQuery.toMpPage();
        //我的ID
        Long UserId = UserContext.getUser();

        Page<Follows> page = lambdaQuery().eq(Follows::getUserId, UserId)
                .page(followsPage);
        List<Follows> list = page.getRecords();
        if (CollUtil.isEmpty(list)) {
            return Result.success("没有了");
        }
        //获取主播的id集合
        List<Long> userids = list.stream().map(Follows::getFollowId).collect(Collectors.toList());
        List<user> list1 = Db.lambdaQuery(user.class)
                .select(user::getId, user::getAvatar, user::getDescription, user::getUsername)
                .in(user::getId, userids)
                .list();
        List<favsuserVO> favsuserVOS = BeanUtil.copyToList(list1, favsuserVO.class);
        return Result.success(favsuserVOS);
    }

    @Override
    public Result followme(PageQuery pageQuery) {
        Page<Follows> followsPage = pageQuery.toMpPage();
        //我的ID
        Long FollowId = UserContext.getUser();

        Page<Follows> page = lambdaQuery().eq(Follows::getFollowId, FollowId)
                .page(followsPage);
        List<Follows> list = page.getRecords();
        if (CollUtil.isEmpty(list)) {
            return Result.success("没有了");
        }
        //获取粉丝的id集合
        List<Long> userids = list.stream().map(Follows::getUserId).collect(Collectors.toList());
        List<user> list1 = Db.lambdaQuery(user.class)
                .select(user::getId, user::getAvatar, user::getDescription, user::getUsername)
                .in(user::getId, userids)
                .list();
        List<favsuserVO> favsuserVOS = BeanUtil.copyToList(list1, favsuserVO.class);
        return Result.success(favsuserVOS);
    }

    @Override
    public Result togerfoller(PageQuery pageQuery) {

        Page<Follows> followsPage = pageQuery.toMpPage();
        //我的ID
        Long FollowId = UserContext.getUser();

        List<Follows> list  = lambdaQuery().eq(Follows::getFollowId, FollowId).list();
        if (CollUtil.isEmpty(list)) {
            return Result.success("没有了");
        }
        //获取粉丝的id集合
        List<Long> userids = list.stream().map(Follows::getUserId).collect(Collectors.toList());
//查我有关注的粉丝

     Page<Follows> page = lambdaQuery()
                .eq(Follows::getUserId, FollowId)
                .in(Follows::getFollowId, userids)
                .page(followsPage);

      List<Follows> records = page.getRecords();
        List<Long> recordsids = records.stream()
                .map(Follows::getFollowId)
                .collect(Collectors.toList());

        List<user> list1 = Db.lambdaQuery(user.class)
                .select(user::getId, user::getAvatar, user::getDescription, user::getUsername)
                .in(user::getId, recordsids)
                .list();
        List<favsuserVO> favsuserVOS = BeanUtil.copyToList(list1, favsuserVO.class);

        return Result.success(favsuserVOS);
    }
}
